package com.wxgo.controller.admin;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.wxgo.common.model.News;
import com.wxgo.common.model.Outtype;
import com.wxgo.common.model.Province;
import com.wxgo.factory.DaoFactory;
import com.wxgo.utils.ExcelKit;
import com.wxgo.utils.OuttypeKit;

public class NewsController extends Controller {

	public void index() {
		render("/admin/news/newsManage.html");
	}

	public void manage() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		Page<News> tablePage = DaoFactory.newsDao().paginate(pageNumber, pageSize, "select n.*,o.type",
				"from news n left join outtype o on o.id=n.outtype");
		renderJson(new Ret().set("code", 0).set("msg", "").set("count", tablePage.getTotalRow()).set("data",
				tablePage.getList()));
	}

	public void edit() {
		keepPara("act");
		if ("edit".equals(getPara("act"))) {
			Integer id = getParaToInt("id");
			News news = DaoFactory.newsDao().findById(id);
			setAttr("news", news);
		}
		List<Province> provinceList = DaoFactory.provinceDao().list();
		setAttr("provinceList", provinceList);
		List<Outtype> outtypeList = Outtype.dao.find("select * from outtype");
		setAttr("outtypeList", outtypeList);
		render("/admin/news/newsEdit.html");
	}

	public void save() {
		News News = getModel(News.class, "news");
		if ("add".equals(getPara("act"))) {
			News.save();
		} else {
			News.update();
		}
		manage();
		return;
	}

	public void delete() {
		String id = getPara("id");
		if ("1".equals(id)) {
			manage();
			return;
		}
		DaoFactory.newsDao().delete("news", id);
		manage();
	}

	public void importBefore() {
		render("/admin/news/import.html");
	}

	public void batchUpdate() {
		System.out.println("batchUpdate start......");
		Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				UploadFile f = getFile("excel");
				File newFile = new File(f.getUploadPath() + "/新途网络-新闻媒体报价单-all.xls");
				
				f.getFile().renameTo(newFile);
				List<String[]> list = ExcelKit.getExcelData(newFile);
				List<News> newsList = new ArrayList<News>();
				OuttypeKit kit = new OuttypeKit();
				int num = list.size();
				System.out.println("共读取到" + num + "条数据。。。。。。");
				System.err.println("start truncate table news....");
				boolean flag = DaoFactory.newsDao().truncate();
				System.out.println("end truncate table news ,flag = " + flag);
				for (String[] strings : list) {
					String name = strings[0];
					if (StrKit.isBlank(name)) {
						flag = DaoFactory.newsDao().saveList(newsList);
						if (!flag) {
							System.out.println("save datas error....");
							return flag;
						}
						newsList.clear();
						break;
					}
					String site = strings[1];
					String province = strings[2];
					String intype = strings[3];
					String outtype = strings[4];
					Double price = Double.parseDouble(strings[5]);

					String remark = strings[6];
					News news = new News();
					news.setIntype("新闻收录".equals(intype) ? 1 : 0);
					news.setNewsname(name);
					news.setOutprice(price);
					news.setOuttype(kit.getId(outtype));
					news.setProvince(province);
					news.setRemark(remark);
					news.setSite(site);
					news.setVipprice(price);
					newsList.add(news);
					// 个数减一
					num--;
					// 判断是否存够300个或者剩余不足300 保存数据
					if (newsList.size() >= 300 || num == 0) {
						flag = DaoFactory.newsDao().saveList(newsList);
						if (!flag) {
							System.out.println("save datas error....");
							return flag;
						}
						newsList.clear();
						System.out.println("leave datas count =" + num);
					}
				}
				flag = DaoFactory.newsDao().updatePcode();
				return flag;
			}
		});
		System.out.println("batchUpdate end......");
		index();
	}

}
